package com.juyu.miliao.api.controller;

import com.alibaba.fastjson.JSONObject;
import com.juyu.miliao.api.domain.ChatSpend;
import com.juyu.miliao.api.service.ChatSpendService;
import com.juyu.miliao.api.service.IntegralLevelInfoService;
import com.juyu.miliao.api.validator.ChatSpendCheck;
import com.juyu.miliao.api.validator.ChatSpendEndCheck;
import com.juyu.miliao.common.domain.CommonHead;
import com.juyu.miliao.common.domain.Subject;
import com.juyu.miliao.common.util.DoubleFormatUtil;
import com.juyu.miliao.common.util.Md5Encryption;
import com.juyu.miliao.common.util.VerifyUtil;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.Authentication;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * 聊天计费
 *
 * @author wbf
 */
@RestController
@RequestMapping(value = "/chatSpend")
@Api(tags = "聊天计费")
public class ChatSpendController {

    @Resource
    ChatSpendService chatSpendService;

    @Resource
    private IntegralLevelInfoService integralLevelInfoService;

    private final Logger logger = LoggerFactory.getLogger("chatSpendRoomId");

    /**
     * 聊天计费/每分钟
     *
     * @param callUserId   拨打电话用户id
     * @param answerUserId 接电话用户id
     * @param record_type  聊天类型：1 语音 2视频
     */
    @PostMapping
    public JSONObject chatSpendMinute(HttpServletRequest request,
                                      HttpServletResponse response,
                                      @Validated(ChatSpendCheck.class) ChatSpend entity) {
        CommonHead com = VerifyUtil.verifyCommonHead(request);
        logger.info("每分钟计费请求------请求体entity:::::"+entity.toString()+"-----请求头CommonHead::::::"+com);
//        int deposit = chatSpendService.chatSpendMinute(entity,com);
        JSONObject json = chatSpendService.chatSpendMinuteByServer(entity,com);
        //2019-10-11 增加混淆
        if(!StringUtils.isEmpty(com.getRanReqPk())){
            response.setHeader("ranReqPk",com.getRanReqPk());
            response.setHeader("ranResPk", Md5Encryption.getDigestStr(com.getRanReqPk()));
        }
        return json;
    }

    /**
     * 聊天结束 统计接口
     *
     * @param callUserId   拨打电话用户id
     * @param answerUserId 接电话用户id
     * @param record_type  聊天类型：1 语音 2视频
     * @param callTime     通话时长(单位/秒)
     */
    @PostMapping("/end")
    public void chatSpendEnd(HttpServletRequest request, @Validated(ChatSpendEndCheck.class) ChatSpend entity) {
        String appPackageId = request.getHeader("appPackageId");
        String phone = request.getHeader("phoneSystem");
        String version = request.getHeader("versionNum");
//        chatSpendService.chatSpendEnd(entity,appPackageId,phone,version);
        //1.5.4 触发器在mq里
    }


    /**
     * 钻石转化率
     * @param token
     * @return
     */
    @GetMapping("/diamondConversionRate")
    public Map<String,String>  getDiamondConversionRate(Authentication token){
        Map<String,String> map = new HashMap<>(0);
        Subject subject = (Subject) token.getPrincipal();
        BigDecimal rate = integralLevelInfoService.getDiamondConversionRate(subject.getId());
        String result=DoubleFormatUtil.doubleFormate(rate.doubleValue())+"";
        map.put("rate",result);
        return map;
    }

    @GetMapping("/call/{roomId}/record")
    public String getCallDetailRecord(@PathVariable("roomId") String roomId){
        return chatSpendService.getCallDetailRecord(roomId);
    }

}
